<?php
class ControllerAccountAddress extends Controller {
	private $error = array();

	public function index() {
	    if (!$this->actionCheck()) {
	        return;
	    }
	    
		$this->load->model('account/address');

		$this->getList();
	}

	public function add() {
		if (!$this->actionCheck(true,true)) {
	        return;
	    }

	    if(!$this->validate())
	    {
	        if (count($this->error)>0) {
	            $error_warning = array_shift($this->error);
	        } else {
	            $error_warning = '';
	        }
	        $this->response->setDataResult(0,$error_warning,null);
	        return;
	    }
	    
		$this->load->model('account/address');
		
		$address_id = $this->model_account_address->addAddress($this->request->post);
		
		
		// Add to activity log
		$this->load->model('account/activity');
		
		$activity_data = array(
		    'customer_id' => $this->customer->getId(),
		    'name'        => $this->customer->getFirstName()
		);
		
		$this->model_account_activity->addActivity('address_add', $activity_data);
		
		$this->response->setDataResult(1,"",$address_id);
        
	}

	public function edit() {
		if (!$this->loginCheck(true)) {
	        return;
	    }

		if(!$this->validate())
	    {
	        if (count($this->error)>0) {
	            $error_warning = array_shift($this->error);
	        } else {
	            $error_warning = '';
	        }
	        $this->response->setDataResult(0,$error_warning,null);
	        return;
	    }


		$this->load->model('account/address');


			$this->model_account_address->editAddress($this->request->get['address_id'], $this->request->post);

			// Default Shipping Address
			if (isset($this->session->data['shipping_address']['address_id']) && ($this->request->get['address_id'] == $this->session->data['shipping_address']['address_id'])) {
				$this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->request->get['address_id']);

				unset($this->session->data['shipping_method']);
				unset($this->session->data['shipping_methods']);
			}

			// Default Payment Address
			if (isset($this->session->data['payment_address']['address_id']) && ($this->request->get['address_id'] == $this->session->data['payment_address']['address_id'])) {
				$this->session->data['payment_address'] = $this->model_account_address->getAddress($this->request->get['address_id']);

				unset($this->session->data['payment_method']);
				unset($this->session->data['payment_methods']);
			}

			$this->session->data['success'] = $this->language->get('text_edit');

			// Add to activity log
			$this->load->model('account/activity');

			$activity_data = array(
				'customer_id' => $this->customer->getId(),
				'name'        => $this->customer->getFirstName()
			);

			$this->model_account_activity->addActivity('address_edit', $activity_data);

			$this->response->setDataResult(1,"",null);
	}

	public function delete() {
	    
		if (!$this->loginCheck()) {
	        return;
	    }

		$this->load->language('account/address');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('account/address');

		if (isset($this->request->get['address_id']) && $this->validateDelete()) {
			$this->model_account_address->deleteAddress($this->request->get['address_id']);

			// Default Shipping Address
			if (isset($this->session->data['shipping_address']['address_id']) && ($this->request->get['address_id'] == $this->session->data['shipping_address']['address_id'])) {
				unset($this->session->data['shipping_address']);
				unset($this->session->data['shipping_method']);
				unset($this->session->data['shipping_methods']);
			}

			// Default Payment Address
			if (isset($this->session->data['payment_address']['address_id']) && ($this->request->get['address_id'] == $this->session->data['payment_address']['address_id'])) {
				unset($this->session->data['payment_address']);
				unset($this->session->data['payment_method']);
				unset($this->session->data['payment_methods']);
			}

			$this->session->data['success'] = $this->language->get('text_delete');

			// Add to activity log
			$this->load->model('account/activity');

			$activity_data = array(
				'customer_id' => $this->customer->getId(),
				'name'        => $this->customer->getFirstName()
			);

			$this->model_account_activity->addActivity('address_delete', $activity_data);

			$this->response->redirect($this->url->link('account/address', '', 'SSL'));
		}

		$this->getList();
	}

	public function setDefault(){
	    if (!$this->actionCheck(true,false)) {
	        return;
	    }
	    $this->load->model('account/address');
	    $this->model_account_address->setDefault($this->request->get['address_id']);
	    $this->response->setDataResult(1,"成功!",null);
	}
	
	protected function getList() {
	    
		$data['addresses'] = array();

		$results = $this->model_account_address->getAddresses();
		
		foreach ($results as $result) {

			$data['addresses'][] = array(
				'address_id' => $result['address_id'],
				'address'    => $result
			);
		}

		$this->response->setDataResult(1,"成功!",$data);
	}

	
	

	protected function validate() {
		if ((utf8_strlen(trim($this->request->post['firstname'])) < 1) || (utf8_strlen(trim($this->request->post['firstname'])) > 32)) {
			$this->error['firstname'] = "请填写联系人姓名！";
		}
		
		if ((utf8_strlen(trim($this->request->post['telephone'])) < 3) || (utf8_strlen(trim($this->request->post['telephone'])) > 128)) {
		    $this->error['telephone'] = "请填写联系人电话！";
		}

		if ((utf8_strlen(trim($this->request->post['address_1']))) ==0) {
			$this->error['address_1'] = "请填写详细地址！";
		}

		if ($this->request->post['country_id'] == ''|| $this->request->post['country_id'] == 0) {
			$this->error['country'] = "请选择省份！";
		}

		if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == 0) {
			$this->error['zone'] = "请选择市区！";
		}
		
		if (!isset($this->request->post['district_id']) || $this->request->post['district_id'] == 0) {
		    $this->error['district'] = "请选择县！";
		}

		return !$this->error;
	}

	protected function validateDelete() {
		if ($this->model_account_address->getTotalAddresses() == 1) {
			$this->error['warning'] = $this->language->get('error_delete');
		}

		if ($this->customer->getAddressId() == $this->request->get['address_id']) {
			$this->error['warning'] = $this->language->get('error_default');
		}

		return !$this->error;
	}
	
	public function country() {
	
	    $this->load->model('localisation/country');
	
	    $countries = $this->model_localisation_country->getCountries();
	
	    $this->response->setDataResult(1,'成功',$countries);
	}
	
	public function zone() {
	
	    $this->load->model('localisation/country');
	
	    $country_info = $this->model_localisation_country->getCountry($this->request->get['country_id']);
	
	    if ($country_info) {
	        $this->load->model('localisation/zone');
	
	        $zones = $this->model_localisation_zone->getZonesByCountryId($this->request->get['country_id']);
	
	        $this->response->setDataResult(1,'成功',$zones);
	        return;
	    }
	
	    $this->response->setDataResult(0,'该省份没有下辖市！',null);
	}
	
	public function district() {
	
	    $this->load->model('localisation/zone');
	
	    $zone_info = $this->model_localisation_zone->getZone($this->request->get['zone_id']);
	
	    if ($zone_info) {
	        $this->load->model('localisation/district');
	
	        $districts = $this->model_localisation_district->getDistrictsByZoneId($this->request->get['zone_id']);
	         
	        $this->response->setDataResult(1,'成功',$districts);
	        return;
	    }
	
	    $this->response->setDataResult(0,'该市没有下辖县！',null);
	}
	
	//获取用户收获默认地址
	public function getlastaddress(){
	    
	    
	}
}
